package com.zhongwang.business.module.cost.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.zhongwang.business.base.BaseController;
import com.zhongwang.business.module.cost.model.Project;
import com.zhongwang.business.module.cost.service.CostService;
import com.zhongwang.business.module.cost.service.DictCommonService;
import com.zhongwang.business.module.cost.service.SheetService;

import com.zhongwang.business.common.JsonResult;
import com.zhongwang.business.module.cost.model.CosSheet;

import com.zhongwang.business.util.ShiroUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.zhongwang.business.constant.Constant.GROUP_SPECTYPE;


/**
 * @Description: 工艺成本业务
 * <p>
 * 权限：
 * dsn:proTasks:view 任务项编辑查看
 * dsn:proTasks:edit 编辑、保存
 * dsn:proTaskinfor:view 任务项查询
 * @Author: China.wsg
 * @Date: 2020/11/10 08:58
 */
@Controller
@RequestMapping("/craft/cost/install")
@SuppressWarnings("all")
public class CraCostInstallController extends BaseController {

    @Autowired
    private CostService costService;
    @Autowired
    private DictCommonService dictCommonService;
    @Autowired
    private SheetService sheetService;

    @GetMapping("/subList")
//    //@RequiresPermissions({"dsn:proTasks:view"})
    public String subPage() {
        return "designtask/list/costSubPage";
    }

    /**
     * 安装录入
     *
     * @param model
     * @return
     */
    @GetMapping("/create")
    @RequiresPermissions({"cost:install:view"})
    public String createPage(Model model) {
        model.addAttribute("special", dictCommonService.findDictByGroupName(GROUP_SPECTYPE));
        return "cost/install/installPage";
    }

    /**
     * 安装查询、提交、修改
     *
     * @param model
     * @return
     */
    @GetMapping("/submit")
    @RequiresPermissions({"cost:instPlan:view"})
    public String submit(Model model) {

        return "cost/install/installSubmit";
    }

    /**
     * 项目编码存在校验
     * 页面级校验
     *
     * @param param
     * @return
     */
    @PostMapping("/codeExist")
    @ResponseBody
    public String codeExist(String proCode) {
        Map<String, Boolean> map = new HashMap<String, Boolean>();
        map.put("valid", costService.codeExist(proCode));
        String resultString = "";
        ObjectMapper mapper = new ObjectMapper();
        try {
            resultString = mapper.writeValueAsString(map);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return resultString;
    }

    /**
     * 项目编码存在校验
     * 页面级校验
     *
     * @param param
     * @return
     */
    @GetMapping("/project")
    @ResponseBody
//    //@RequiresPermissions({"dsn:proTasks:view"})
    public List<Project> findProject(String proCode) {
        return costService.findProjectByCode(proCode);
    }

    /**
     * 保存安装录入
     */
    @ResponseBody
    @PostMapping("/save")
    @RequiresPermissions({"cost:install:edit"})
    public JsonResult saveMake(CosSheet cosSheet, Double[] wQuantity, String[] wUnit, String[] wDescribe, String[] wRemark, Double[] hQuantity, String[] hUnit, String[] hDescribe, String[] hRemark) {
        cosSheet.setCostType(7);//安装工时
        if (wDescribe == null && hDescribe == null) {
            return JsonResult.success("请最少录入一条数据！", -1);
        }
        int i = costService.saveBlanking(cosSheet, wQuantity, wUnit, wDescribe, wRemark, hQuantity, hUnit, hDescribe, hRemark);
        if (-2 == i) {
            return JsonResult.success("当前项目下的专业和部分已经提报过了！请走变更流程！", -2);
        }
        return jsonResult(1);
    }

    /**
     * 保存安装修改
     */
    @ResponseBody
    @PostMapping("/saveEdit")
    //@RequiresPermissions({"cost:installing:edit"})
    public JsonResult saveEdit(CosSheet cosSheet, Double[] wQuantity, String[] wUnit, String[] wDescribe, String[] wRemark, Double[] hQuantity, String[] hUnit, String[] hDescribe, String[] hRemark) {
        cosSheet.setCostType(7);//安装工时
        if (wDescribe == null && hDescribe == null) {
            return JsonResult.success("请最少录入一条数据！", -1);
        }
        int i = costService.saveEditBlanking(cosSheet, wQuantity, wUnit, wDescribe, wRemark, hQuantity, hUnit, hDescribe, hRemark);
        if (-2 == i) {
            return JsonResult.success("当前项目下的专业和部分已经提报过了！请走变更流程！", -2);
        }
        return jsonResult(1);
    }

    /**
     * 列表查询
     */
    @ResponseBody
    @PostMapping("/list")
//    //@RequiresPermissions({"report:query:view"})
    public JsonResult list(CosSheet cosSheet) {
        startPage();
        cosSheet.setCreateCode(ShiroUtils.getUser().getAccount());
        cosSheet.setCostType(7);// 安装工时
        List<CosSheet> list = costService.findCosSheetByQuery(cosSheet);
        return jsonResult(list);
    }

    /**
     * 安装修改
     *
     * @param model
     * @return
     */
    @GetMapping("/edit/{id}")
    @RequiresPermissions({"cost:installing:edit"})
    public String edit(@PathVariable("id") Integer id, Model model) {
        CosSheet cosSheet = costService.getCosSheetById(id);
        model.addAttribute("cosSheet", cosSheet);
        model.addAttribute("special", dictCommonService.findDictByGroupName(GROUP_SPECTYPE));
        model.addAttribute("proPart", sheetService.findPartByProjectCode(cosSheet.getProCode(), "外观".equals(cosSheet.getSpecial()) ? null : cosSheet.getSpecial()));
        model.addAttribute("hCosDetail", costService.findCosDetailByCosSheetIdAndDataType(id, "'人工工时'"));
        model.addAttribute("wCosDetail", costService.findCosDetailByCosSheetIdAndDataType(id, "'主要辅材'"));
        return "cost/install/installEdit";
    }

    /**
     * 安装删除
     *
     * @param model
     * @return
     */
    @ResponseBody
    @PostMapping("/del")
    @RequiresPermissions({"cost:install:del"})
    public JsonResult del(Integer id, Model model) {

        return jsonResult(costService.delCostById(id, "安装工时计划单"));
    }

    /**
     * 安装提交
     *
     * @param model
     * @return
     */
    @ResponseBody
    @PostMapping("/submit")
    @RequiresPermissions({"cost:install:commit"})
    public JsonResult submit(Integer id, Model model) {

        return jsonResult(costService.submitCost(id, "安装工时计划单"));
    }

    /**
     * 安装查询、提交、修改页面
     *
     * @param model
     * @return
     */
    @GetMapping("/queryDetail/{id}")
//    //@RequiresPermissions({"dsn:proTasks:view"})
    public String queryDetail(@PathVariable("id") Integer id, Model model) {
        CosSheet cosSheet = costService.getCosSheetById(id);
        model.addAttribute("cosSheet", cosSheet);
        model.addAttribute("special", dictCommonService.findDictByGroupName(GROUP_SPECTYPE));
        model.addAttribute("proPart", sheetService.findPartByProjectCode(cosSheet.getProCode(), "外观".equals(cosSheet.getSpecial()) ? null : cosSheet.getSpecial()));
        model.addAttribute("hCosDetail", costService.findCosDetailByCosSheetIdAndDataType(id, "'人工工时'"));
        model.addAttribute("wCosDetail", costService.findCosDetailByCosSheetIdAndDataType(id, "'主要辅材'"));
        return "cost/install/installQueryDetail";
    }

    /**
     * 安装工时计划单审核页面
     *
     * @return
     */
    @GetMapping("/examine")
    @RequiresPermissions({"cost:instalList:view"})
    public String checkPage() {
        return "cost/install/installExamine";
    }

    /**
     * 待审核列表
     */
    @ResponseBody
    @PostMapping("/examine")
//    //@RequiresPermissions({"tec:list:view"})
    public JsonResult checkList(CosSheet cosSheet) {
        startPage();
        cosSheet.setCostType(7);// 安装工时计划单
        cosSheet.setPageQuery(3);// 审核页面
//        cosSheet.setVerifyId(ShiroUtils.getUser().getId());
        List<CosSheet> list = costService.findCosSheetByQuery(cosSheet);
        return jsonResult(list);
    }
}
